﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Transformer.iHunter.UI
{
    public partial class FieldTextBox : TextBox
    {
        private IFieldTextBoxOwner owner;
        private string tipContent;
        private ToolTip tip;
        private bool enableTip;
        private bool tipManullyShowed = false;
        public bool EnableTip
        {
            set{
                enableTip = value;
                tip.Active = enableTip;
            }
        }

        public ToolTip Tip
        {
            get { return tip; }
        }


        
        public FieldTextBox()
        {     

            InitializeComponent();            
        }

        public void SetOwner(IFieldTextBoxOwner editor, string tipContent)
        {
            this.owner = editor;
            this.tipContent = tipContent;
            this.tip = new ToolTip();
            
            tip.IsBalloon = true;
            this.EnableTip = false;

            this.tip.SetToolTip(this, tipContent);
        }

        
        public FieldTextBox(IFieldTextBoxOwner editor, string tipContent)
        {
            
            InitializeComponent();

            SetOwner(editor, tipContent);
        }

        public void ShowTip(string content)
        {
            tip.Active = true;
            
            this.tip.Show(content, this, 500);

            tipManullyShowed = true;
        }

        public void SetTipContent(string cont)
        {
            tipContent = cont;

            this.tip.SetToolTip(this, cont);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        private void FieldTextBox_TextChanged(object sender, EventArgs e)
        {            
            owner.MarkMessageModified();

            if (tipManullyShowed)
            {
                tipManullyShowed = false;
                tip.Active = enableTip;
                tip.SetToolTip(this, tipContent);
            }
            
        }

        private void FieldTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            owner.EnableClipboardButtons(this.SelectionLength > 0, this.CanUndo, false);
        }

        private void FieldTextBox_Enter(object sender, EventArgs e)
        {
            owner.EnterFieldTextBox(this);
        }

        private void FieldTextBox_Click(object sender, EventArgs e)
        {
            owner.EnableClipboardButtons(this.SelectionLength > 0, this.CanUndo, false);
        }

        private void FieldTextBox_MouseUp(object sender, MouseEventArgs e)
        {
            owner.EnableClipboardButtons(this.SelectionLength > 0, this.CanUndo, false);
        }
    }
}
